----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 11205 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Snipe"
SPELL.m_sToolTip =	[[You take aim, firing
					a precise and deadly shot
					towards your target.]]
SPELL.m_sIcon = "devin/blackbetty/spells/ranger/snipe.png"
SPELL.m_iCoolDown = 30
SPELL.m_iManaCost = 0
SPELL.m_bRequiresTarget = true
SPELL.m_iSpellType = SPELLTYPE_RANGEDAMAGE
SPELL.m_sUsedBy = "class_ranger"

---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )
	
	print("Casted "..self.m_sName.."!")
	
	local range = self.m_iRange
		
	if(target && target:IsValid() && target != pl) then
		pl:EmitSound("weapons/mortar/mortar_shell_incomming1.wav", 35, 100+math.random(-10,10))
		pl:EmitSound("devin/blackbetty/weapons/castsounds/FireCast.wav", 40, 100+math.random(-10,10))
		ParticleEffectAttach( "impact_firebig", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
		timer.Simple(.9, function()
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
		end)
		net.Start( "SendPlayerCast" )
		local SendTable = {1.5, self.m_sName, Color(15, 15, 255, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		pl.DoingCast = true
		pl.CastWhileMoving = true
		pl:EmitSound("devin/blackbetty/weapons/bow/BowPullback0"..math.random(1,3)..".wav", 70, 100+math.random(-20,-10))
			timer.Simple(1.5, function()
				if(pl:GetPos():Distance(target:GetPos()) < range)then
					self:Effect(pl, target, range)
					--self:ApplySpellMods(self, pl, target)
				end
			end)
		else
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			pl.DoingCast = false
			return true
	end
end

function SPELL:Effect(pl, target)

	local weapon = pl:GetWeapon("weapon_all")
	
	pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_CROSSBOW)
	pl:EmitSound("devin/blackbetty/weapons/bow/BowRelease01.wav", 50, 100+math.random(-10,10))
	pl:EmitSound("weapons/mortar/mortar_fire1.wav", 60, 100+math.random(-10,10))
	UpdatePlayerResource(pl, 20, true)
	if(SERVER)then
		ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
		local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/2100)
		LaunchProjectile(pl, target, 0, "pummel_hit", "", "snipe", "devin/blackbetty/weapons/travelsounds/FireMissileLoop.wav", myhitdelay, 2400, "models/crossbow_bolt.mdl")
		timer.Simple(myhitdelay, function()
			local dmginfo = DamageInfo()
				if(weapon)then
					dmginfo:SetDamage( math.random(weapon.MinWeaponDamage*6, weapon.MaxWeaponDamage*6) + math.Round((weapon.MaxWeaponDamage*1.5)*(pl:GetPos():Distance(target:GetPos())/self.m_iRange))  )
				else
					dmginfo:SetDamage( math.random(pl.MinDamage, pl.MaxDamage) ) 
				end
				dmginfo:SetDamageType( DMG_BURN ) 
				dmginfo:SetAttacker( pl )
			target:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - target:GetPos())*-2)
			target:TakeDamageInfo( dmginfo )
			target:EmitSound("devin/blackbetty/weapons/bow/ArrowHit0"..math.random(1,3)..".wav", 100, 100+math.random(-10,10))
			target:EmitSound("ambient/explosions/explode_1.wav", 150, 100)
			ParticleEffect( "impact_firebig", target:GetPos() + Vector(0, 0, 32), Angle(0,0,0), pl )
			ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )
			
		end)
	end
end




